<template>
  <basic-container>
    <avue-crud :option="option"
               :table-loading="loading"
               :data="data"
               :page="page"
               :permission="permissionList"
               :before-open="beforeOpen"
               v-model="form"
               ref="crud"
               @row-update="rowUpdate"
               @row-save="rowSave"
               @row-del="rowDel"
               @search-change="searchChange"
               @search-reset="searchReset"
               @selection-change="selectionChange"
               @current-change="currentChange"
               @size-change="sizeChange"
               @on-load="onLoad">
      <template slot="menu">
      </template>
      <template slot-scope="{row}" slot="status">
        <el-tag v-if="row.status === 1" type="success">已支付</el-tag>
        <el-tag v-else-if="row.status === 0" type="danger">未支付</el-tag>
        <el-tag v-else-if="row.status === 2" type="danger">关闭</el-tag>
        <el-tooltip v-else-if="row.status === 3"
                    effect="dark"
                    :content="`退款时间: ${row.refundTime}  备注: ${row.refundRemark}`"
                    placement="top">
          <el-tag type="danger">已退款</el-tag>
        </el-tooltip>
      </template>
      <template slot-scope="{row}" slot="payType">
        <el-tag v-if="row.payType === 1" type="success">微信</el-tag>
        <el-tag v-else-if="row.payType === 2" type="danger">线下收款</el-tag>
        <el-tag v-else type="danger">未知</el-tag>
      </template>
      <template slot-scope="{row}" slot="isSingle">
        <el-tag v-if="row.isSingle === 1" type="primary">单餐</el-tag>
        <el-tag v-else-if="row.isSingle === 2" type="success">多餐</el-tag>
        <el-tag v-else type="danger">未知</el-tag>
      </template>
      <template slot-scope="{row}" slot="memberName">
        <a class="el-link" style="color: #2d8cf0" @click="openDetail(row)">{{ row.memberName }}</a>
      </template>
      <template slot="menu" slot-scope="{row}">
        <el-button size="small"
                   v-if="[1].includes(row.status) && permissionList.refund"
                   type="text"
                   @click="handleRefund(row)">退款
        </el-button>
      </template>
    </avue-crud>
    <el-dialog title="详情"
               append-to-body
               :visible.sync="detailVisible"
               :close-on-click-modal="false"
               :close-on-press-escape="false"
               width="80%">
      <member-detail v-if="detailVisible" :id="memberId" :tab="3"></member-detail>
    </el-dialog>
  </basic-container>
</template>

<script>
  import {getList, getDetail, add, update, remove, refund} from "@/api/packages/packages_order";
  import {mapGetters} from "vuex";
  import MemberDetail from "../member/member_detail";

  export default {
    components: { MemberDetail },
    data() {
      return {
        detailVisible: false,
        memberId: '',
        form: {},
        query: {},
        loading: true,
        page: {
          pageSize: 100,
          currentPage: 1,
          total: 0
        },
        selectionList: [],
        option: {
          height: 'auto',
          calcHeight: 210,
          searchShow: true,
          searchMenuSpan: 6,
          tip: false,
          menuWidth: 100,
          border: true,
          index: true,
          viewBtn: false,
          addBtn: false,
          refreshBtn: false,
          editBtn: false,
          delBtn: false,
          selection: true,
          column: [
            {
              label: "套餐",
              prop: "packagesId",
              search: true,
              type: "select",
              dicUrl: '/api/celery-packages/packages/select',
              props: {
                label: "name",
                value: "id"
              },
            },
            {
              label: "会员用户",
              prop: "memberName",
              slot: true,
            },
            {
              label: "金额",
              prop: "amount",
            },
            {
              label: "支付方式",
              prop: "payType",
              slot: true,
            },
            {
              label: "是否单餐",
              prop: "isSingle",
              slot: true,
            },
            {
              label: "剩余有效餐时",
              prop: "effectiveCount",
            },
            {
              label: "套餐总餐时",
              prop: "totalCount",
            },
            {
              label: "时间",
              prop: "createTime",
            },
            {
              label: "状态",
              prop: "status",
              slot: true,
            },
          ]
        },
        data: []
      };
    },
    computed: {
      ...mapGetters(["permission"]),
      permissionList() {
        return {
          addBtn: this.vaildData(this.permission.packages_order_add, false),
          viewBtn: this.vaildData(this.permission.packages_order_view, false),
          delBtn: this.vaildData(this.permission.packages_order_delete, false),
          editBtn: this.vaildData(this.permission.packages_order_edit, false),
          refund: this.vaildData(this.permission.refund, false)
        };
      },
      ids() {
        let ids = [];
        this.selectionList.forEach(ele => {
          ids.push(ele.id);
        });
        return ids.join(",");
      }
    },
    methods: {
      openDetail(row) {
        this.memberId = row.memberId
        this.detailVisible = true
      },
      handleRefund(row) {
        this.$confirm("套餐内有效餐时将清零，不可撤回，退款后该套餐关联已有的选餐不可取消，确定退款该套餐订单?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return refund(row.id, "人工退款");
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
          });
      },
      rowSave(row, done, loading) {
        add(row).then(() => {
          done();
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        }, error => {
          window.console.log(error);
          loading();
        });
      },
      rowUpdate(row, index, done, loading) {
        update(row).then(() => {
          done();
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        }, error => {
          window.console.log(error);
          loading();
        });
      },
      rowDel(row) {
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(row.id);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
          });
      },
      handleDelete() {
        if (this.selectionList.length === 0) {
          this.$message.warning("请选择至少一条数据");
          return;
        }
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(this.ids);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
            this.$refs.crud.toggleSelection();
          });
      },
      beforeOpen(done, type) {
        if (["edit", "view"].includes(type)) {
          getDetail(this.form.id).then(res => {
            this.form = res.data.data;
          });
        }
        done();
      },
      searchReset() {
        this.query = {};
        this.onLoad(this.page);
      },
      searchChange(params, done) {
        this.query = params;
        this.page.currentPage = 1;
        this.onLoad(this.page, params);
        done();
      },
      selectionChange(list) {
        this.selectionList = list;
      },
      selectionClear() {
        this.selectionList = [];
        this.$refs.crud.toggleSelection();
      },
      currentChange(currentPage){
        this.page.currentPage = currentPage;
      },
      sizeChange(pageSize){
        this.page.pageSize = pageSize;
      },
      onLoad(page, params = {}) {
        this.loading = true;
        this.query.orderType = 1
        getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
          const data = res.data.data;
          this.page.total = data.total;
          this.data = data.records;
          this.loading = false;
          this.selectionClear();
        });
      }
    }
  };
</script>

<style>
</style>
